﻿/*
    知识结构删除弹框
        调用此弹框需要用ng-if



*/
angular.module("knowledgeDeleteBox", ['Common', 'knowledgebaseS'])
.directive("knowledgeDeleteBox", ['$state', 'sysUserInfo', 'RequestTrainService', 'Comm', 'MsgCon', 'Msg', function ($state, sysUserInfo, RTS, Comm, MsgCon, Msg) {
    return {
        restrice: "AE",
        scope: {
            delDataList: "=",      //数据绑定
            isHide: "&",     // 由于用ng-if 所以取消时调用父窗口的方法
            delAll: "&",
            ids: "=", //课程id，试卷id
            type: "="//1课程，共享课程2试卷3知识架构
        },
        templateUrl: "/system/directive/train/knowledgedeletebox/knowledge_delete_box.html",
        link: function (scope, element, attr) {
            console.log(scope.delDataList);

            // 处理数据筛选出 课程 试卷 任务
            scope.courseList = [];
            scope.examList = [];
            scope.taskList = [];
            scope.questionList = []; //题库

            //处理任务的json，吧课程或试卷删除
            scope.delArrangeType = function (stobj) {
                var count = parseInt(stobj.courser_count);
        //        console.log("删除前的课程数：" + count);
                stobj.arragetype = JSON.parse(stobj.arragetype);
                stobj.complete = JSON.parse(stobj.complete);
                //item.value
                for (var i = 0; i < scope.ids.length; i++) {
                    stobj.arragetype.forEach(function (item, index) {
                        if (scope.type == 1) {
                            item.kscList.forEach(function (course, cindex) {
                                if (course.course_Id == scope.ids[i]) {
                                    item.kscList.splice(cindex, 1);
                                    count = count - 1;
                                }
                            });
                        } else if (scope.type == 2) {
                            item.kseList.forEach(function (paper, pindex) {
                                if (paper.paperId == scope.ids[i]) {
                                    item.kseList.splice(pindex, 1);
                                    count = count - 1;
                                }
                            });
                            //知识架构
                        } else if (scope.type == 3) {
                            item.kssList.forEach(function (know, kindex) {
                                if (know.knowledge_Id == scope.ids[i]) {
                                    item.kssList.splice(kindex, 1);
                                    //count = count - 1;//知识机构前端暂时没法计算课程量，先不算
                                }
                            });

                        }
                        item.value = "";
                        stobj.complete.courseList = [];
                        stobj.complete.examList = [];
                        stobj.complete.queList = [];
                        //重新赋值value
                        item.kscList.forEach(function (course, cindex) {
                            item.value += (item.value.length > 0 ? ',' : '') + course.course_Id;
                            stobj.complete.courseList.push(course.course_Id);
                        });
                        item.kseList.forEach(function (paper, pindex) {
                            item.value += (item.value.length > 0 ? ',' : '') + paper.paperId;
                            stobj.complete.examList.push(paper.paperId);
                        });
                        item.kssList.forEach(function (know, pindex) {
                            item.value += (item.value.length > 0 ? ',' : '') + know.knowledge_Id;
                            stobj.complete.queList.push(know.knowledge_Id);
                        });
                    });
                }
                stobj.courser_count = count;

             //   console.log("删除后的课程数：" + stobj.courser_count);
            }
            angular.forEach(scope.delDataList, function (data, index, array) {
                if (data.hasOwnProperty("course_Id")) { // 课程
                    scope.courseList.push(data);
                }
                if (data.hasOwnProperty("paperId") || data.hasOwnProperty("randompaper_id")) { // 试卷
                    scope.examList.push(data);
                }
                if (data.hasOwnProperty("id")) { // 任务
                    scope.delArrangeType(data);
                    scope.taskList.push(data);
                }
                if (data.hasOwnProperty("questions_Id")) { // 题
                    scope.questionList.push(data);
                }
            });

            //取消删除
            scope.isHideFun = function () {
                scope.isHide();
            }
            //继续删除
            scope.delAllFun = function () {
                scope.delAll();
            }
            //删除关系
            scope.delRealtionFun = function () {
                scope.isloading = true;
                var count = 0;
                scope.taskList.forEach(function (data, index) {
                    RTS.UpdateStageJson({ id: data.id, json: JSON.stringify(data.arragetype), complete: JSON.stringify(data.complete), courseCount: data.courser_count, userid: sysUserInfo.getuserinfo().user_ID }).then(function (data) {
                        count += 1;
                        if (data.errorcode == "0") {
                            Msg.success("解绑成功！");
                        }
                        scope.isloading = false;
                        //删除完关系删除课程
                        if (count >= scope.taskList.length) {
                            scope.isHide();
                            scope.delAll();
                        }
                    });
                }, function () { scope.isloading = false; });
            }
        }
    }
} ]);


